import { getCode, getNanoid } from '@/utils';
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { JvmService } from './jvm.service';

@Injectable()
export class NbcbService {
  private readonly custId: string;
  private readonly privateKey: string;
  private readonly configFilePath: string;
  private readonly corpCode: string;
  private readonly payAcc: string;
  private readonly sdk: any;
  constructor(
    private readonly config: ConfigService,
    private readonly jvmService: JvmService,
  ) {
    this.custId = this.config.get('nbcb.custId');
    this.privateKey = this.config.get('nbcb.privateKey');
    this.configFilePath = this.config.get('nbcb.configFilePath');
    this.corpCode = this.config.get('nbcb.corpCode');
    this.payAcc = this.config.get('nbcb.payAcc');
    this.sdk = this.jvmService.import('com.nbcb.sdk.OpenSDK');
    this.sdk.initSync(this.configFilePath, this.privateKey);
  }

  async generateSerialNo() {
    return (await getCode(4)) + (await getNanoid(4));
  }

  async queryAccount(data: any) {
    return new Promise((r) => {
      r(
        JSON.parse(
          this.sdk.sendSync(
            'tmsReport',
            'queryAccount',
            JSON.stringify({
              Data: {
                custId: this.custId,
                ...data,
              },
            }),
          ),
        ),
      );
    });
  }

  async singleTransfer(data: any): Promise<any> {
    return new Promise((r) => {
      r(
        JSON.parse(
          this.sdk.sendSync(
            'tmsSettle',
            'singleTransfer',
            JSON.stringify({
              Data: {
                custId: this.custId,
                corpCode: this.corpCode,
                payAcc: this.payAcc,
                ...data,
              },
            }),
          ),
        ),
      );
    });
  }

  async querySingleTransferResult(data: any): Promise<any> {
    return new Promise((r) => {
      r(
        JSON.parse(
          this.sdk.sendSync(
            'tmsSettle',
            'querySingleTransferResult',
            JSON.stringify({
              Data: {
                custId: this.custId,
                ...data,
              },
            }),
          ),
        ),
      );
    });
  }

  async queryRefund(data: any): Promise<any> {
    return new Promise((r) => {
      r(
        JSON.parse(
          this.sdk.sendSync(
            'tmsSettle',
            'queryRefund',
            JSON.stringify({
              Data: {
                custId: this.custId,
                ...data,
              },
            }),
          ),
        ),
      );
    });
  }

  async getGeneralDownloadUrl(data: any) {
    return new Promise((r) => {
      r(
        JSON.parse(
          this.sdk.sendSync(
            'tms',
            'getGeneralDownloadUrl',
            JSON.stringify({
              Data: {
                custId: this.custId,
                ...data,
              },
            }),
          ),
        ),
      );
    });
  }
}
